﻿using NHibernate;
using OneStationFile.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OneStationFile.DAL
{
    public class BaseDao : IBaseDao
    {
        static ISession session = FluentNHibernateHelper.GetSession();
        //static ITransaction trans = session.BeginTransaction();
        #region 根据id 获取实体
        /// <summary>
        /// 根据id 获取实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetModelById<T>(int id)
        {
            //session.Clear();
            var model = session.Get<T>(id);
            return model;
        }
        #endregion
        #region 添加实体
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool SaveOrUpdate<T>(T t)
        {
            //using (var trans = session.BeginTransaction())
            //{
            //    try
            //    {
            //        session.SaveOrUpdate(t);
            //        session.Flush();
            //        trans.Commit();
            //        return true;
            //    }
            //    catch (Exception)
            //    {
            //        trans.Rollback();
            //        return false;
            //    }
            //}
            try
            {
                session.SaveOrUpdate(t);
                session.Flush();
                return true;
            }
            catch (Exception ex)
            {

                return false;
            }
           
        }
        #endregion
        #region 获取所有models
        /// <summary>
        /// 获取所有models
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IQueryOver<T> GetModels<T>() where T:class
        {
            //session.Clear();
            return  session.QueryOver<T>();
        }

        #endregion
        #region 根据条件查询记录数
        /// <summary>
        /// 根据条件查询记录数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public int ExistModelByWhere<T>(Expression<Func<T,bool>> expression) where T :class {
            int count =  session.QueryOver<T>().Where(expression).List().Count();
            return count;
        }
        #endregion
        #region 根据条件查询默认的单个model
        /// <summary>
        /// 根据条件查询默认的model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public T GetModelByWhere<T>(Expression<Func<T, bool>> expression) where T : class
        {
            T t = session.QueryOver<T>().Where(expression).List().FirstOrDefault();
            return t;
        }
        #endregion
        #region 根据条件查询所有model
        /// <summary>
        /// 根据条件查询所有model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public List<T> GetModelsByWhere<T>(Expression<Func<T, bool>> expression) where T : class
        {
            List<T> listT = session.QueryOver<T>().Where(expression).List().ToList();
            return listT;
        }
        #endregion
        #region 根据id删除记录
        /// <summary>
        /// 根据id删除记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteModelById<T>(int id) {
            try
            {
                T t = session.Get<T>(id);
                session.Delete(t);
                session.Flush();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
            
        }
        #endregion

    }
}
